﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web.Mvc;

namespace HitchHikersGuideToTheGalaxy.Models
{

    [Bind(Include = "Id,DestinationIdTo,DestinationIdFrom,Time,VehicleDriverId,SmokingAllowed,PetAllowed,AvailableSeats,Distance,Cost")]
    public partial class Tour
    {
        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Id.ToString()))
                yield return new RuleViolation("Id is required","Id");

            if (String.IsNullOrEmpty(DestinationIdTo.ToString()))
                yield return new RuleViolation("DestinationIdTo is required", "DestinationIdTo");

            if (String.IsNullOrEmpty(DestinationIdFrom.ToString()))
                yield return new RuleViolation("DestinationIdFrom is required", "DestinationIdFrom");

            if (String.IsNullOrEmpty(Time.ToString()))
                yield return new RuleViolation("Time is required","Time");

            if (String.IsNullOrEmpty(VehicleDriverId.ToString()))
                yield return new RuleViolation("VehicleDriverId is required", "VehicleDriverId");

            if (String.IsNullOrEmpty(SmokingAllowed.ToString()))
                yield return new RuleViolation("SmokingAllowed is required", "SmokingAllowed");

            if (String.IsNullOrEmpty(PetAllowed.ToString()))
                yield return new RuleViolation("PetAllowed is required", "PetAllowed");

            if (String.IsNullOrEmpty(AvailableSeats.ToString()))
                yield return new RuleViolation("AvailableSeats is required", "AvailableSeats");

            if (String.IsNullOrEmpty(Distance.ToString()))
                yield return new RuleViolation("Distance is required", "Distance");

            if (String.IsNullOrEmpty(Cost.ToString()))
                yield return new RuleViolation("Cost is required", "Cost");

            //if (String.IsNullOrEmpty(Route))
            //    yield return new RuleViolation("Route is required", "Route");

            //if (String.IsNullOrEmpty(Note))
            //    yield return new RuleViolation("Note is required", "Note");

            yield break;
        }

        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }

    }

}